package com.briup.day15;

import java.util.Set;
import java.util.TreeSet;

public class Test_TreeSet {
    public static void main(String[] args) {
        Set<Person> set = new TreeSet<>();
        Person s1 = new Person("zs", 10);
        Person s2 = new Person("ls", 89);
        Person s4 = new Person("ls", 20);
        Person s3 = new Person("ww", 34);
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        System.out.println(set);
        for (Person p : set) {
            System.out.println(p);
        }
        //如果没有指定排序规则，TreeSet无法进行排序
    }
        public static void main01(String[] args) {
        //自动排序
        Set<Integer> set = new TreeSet<Integer>();
        set.add(1);
        set.add(23);
        set.add(9);
        set.add(8);
        set.add(6);
        System.out.println(set.size());
        System.out.println(set);
        for (Integer integer : set) {
            System.out.println(integer);
        }
    }
}
class Person implements Comparable<Person>{
    private String name;
    private int age;

    @Override
    public int compareTo(Person o) {
        int r=this.name.compareTo(o.name);
        if(r==0) {
            r=o.age-this.age;
        }
            return r;
    }

    public Person(String name, int age){
        this.name=name;
        this.age=age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public void setName(String name){
        this.name=name;
    }
    public void setAge(int age){
        this.age=age;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}